/*
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.submarine.transport;

import io.netty.channel.ChannelHandlerContext;

import org.submarine.exchange.TransferObject;
import org.submarine.exchange.adapter.ExchangeAdapter;


/**
 * 
 * @file org.submarine.transport.Endpoint.java
 *
 * 端点根接口
 * 
 * @author gaojian
 * @created 2013-8-17 下午11:10:09
 * @version 0.0.1
 * @since 0.0.1
 *
 */
public abstract class Endpoint {
	
	private ExchangeAdapter exchangeAdapter;
	
	public Endpoint(ExchangeAdapter exchangeAdapter) {
		this.exchangeAdapter = exchangeAdapter;
	}
	
	/**
	 * 
	 * 启动
	 *
	 * @author gaojian
	 * @created 2013-8-19 上午11:13:55
	 *
	 */
	public abstract void start();
	
	/**
	 * 
	 * 停止
	 *
	 * @author gaojian
	 * @created 2013-8-19 上午11:14:08
	 *
	 */
	public abstract void stop();
	
	protected ExchangeAdapter getExchangeAdapter() {
		return exchangeAdapter;
	}
	
	protected void processedMessageRecived(ChannelHandlerContext ctx, TransferObject transferObject) {
		if (null == transferObject.getTransferObjectType()) {
			return;
		} else {
			switch (transferObject.getTransferObjectType()) {
			case REQUEST: {
				processRequest(ctx, transferObject);
				break;
			}
			case RESPONSE: {
				processResponse(ctx, transferObject);
				break;
			}
			default: {
				break;
			}
			}
		}
	}
	
	private void processRequest(ChannelHandlerContext ctx, TransferObject transferObject) {
		getExchangeAdapter().recived(transferObject);
	}
	
	private void processResponse(ChannelHandlerContext ctx, TransferObject transferObject) {
		getExchangeAdapter().recived(transferObject);
	}
	
}
